package cn.hzking.gjmall.activitys.coupon;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fingdo.statelayout.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.adpters.coupon.CouponListAdapter;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.CouponBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.view.SimpleOptionView;

/**
 * item_coupon
 */
public class CouponListActivity extends ColorStatuBarActivity implements CouponListAdapter.CouponInterface{

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.refrsh)
    SwipeRefreshLayout refrsh;
    @Bind(R.id.statlayout)
    StateLayout statlayout;
    private String skuIds;
    private String numbers;
    private ArrayList<CouponBean.DataBean.CouponSkuIdsArrayBean.CouponBean1> list = new ArrayList<>();
    private ArrayList<List<Integer>> skuIdlist = new ArrayList<>();
    private CouponListAdapter couponListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);
        ButterKnife.bind(this);
        simpleTileView.setTitle("选择优惠券");
        Intent intent = getIntent();
        skuIds = intent.getStringExtra("skuIds");
        numbers = intent.getStringExtra("numbers");
        initView();
        getNetData();
    }

    private void initView() {
       // SwipeRefreshLayoutUtils.initRefreshLayout(refrsh);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        couponListAdapter = new CouponListAdapter(null);
        couponListAdapter.setCouponInterface(this);
        recyclerview.setAdapter(couponListAdapter);

    }

    public void getNetData() {
        RetrofitFactory.getInstant().API()
                .getCouponList(skuIds,numbers)
                .compose(this.<CouponBean>setThread())
                .subscribe(new BaseObserver<CouponBean>(this,true,false) {
                    @Override
                    protected void onSuccees(CouponBean couponBean) throws Exception {
                        if(couponBean != null) {
                            sortData(couponBean);
                        }

                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }

                    @Override
                    protected void onCodeError(CouponBean couponBean) throws Exception {
                        super.onCodeError(couponBean);
                    }
                });

    }

    private void sortData(CouponBean couponBean) {
        List<CouponBean.DataBean.CouponSkuIdsArrayBean> couponSkuIdsArray = couponBean.getData().getCouponSkuIdsArray();
        ArrayList<CouponBean.DataBean.CouponSkuIdsArrayBean.CouponBean1> listt = new ArrayList<>();
        if(couponSkuIdsArray != null){
            skuIdlist.clear();
            for (int i = 0; i < couponSkuIdsArray.size(); i++) {
                listt.add(couponSkuIdsArray.get(i).getCoupon());
                skuIdlist.add(couponSkuIdsArray.get(i).getSkuIds());
            }
        }
        couponListAdapter.setNewData(listt);
    }


    @Override
    public void select(int couponId,String couponName,int position) {
        List<Integer> idlist=skuIdlist.get(position);
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i <idlist.size() ; i++) {
            sb.append(idlist.get(i));
            if(i!=idlist.size()-1){
                sb.append(",");
            }
        }
        String couponSkuIds=sb.toString().trim();
        Intent intent=new Intent();
        intent.putExtra("couponId",""+couponId);
        intent.putExtra("couponName",couponName);
        intent.putExtra("couponSkuIds",couponSkuIds);
        CouponListActivity.this.setResult(RESULT_OK, intent);
        CouponListActivity.this.finish();
    }
}
